//
//  EmployeeManagementViewController.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/7/3.
//

import UIKit
import CollectionKit

/// 员工管理
class EmployeeManagementViewController: UIViewController {

  var collectionView = CollectionView()

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    opacityNavigationBar(color: .designKit.background)
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    title = "员工管理"
    view.backgroundColor = .designKit.secondaryBackground

    view.addSubview(collectionView)

    collectionView.snp.makeConstraints { maker in
      maker.edges.equalToSuperview()
    }

    let dataSource = ArrayDataSource<EmployeeManagementModel>(
      data: EmployeeManagementModel.items
    )

    let viewSource = ClosureViewSource<EmployeeManagementModel, UnitImageTextView> { item, data, index in
      item.text = data.name
      item.font = .designKit.bodyBold
    }

    let sizeSource = { (index: Int, data: EmployeeManagementModel, size: CGSize) -> CGSize in
      if index == 0 {
        return CGSize(width: size.width, height: 80)
      }
      return CGSize(width: (size.width - 12) / 2, height: 80)
    }

    let itemProvider = BasicProvider<EmployeeManagementModel, UnitImageTextView>(
      dataSource: dataSource,
      viewSource: viewSource,
      sizeSource: sizeSource,
      layout: FlowLayout(lineSpacing: 12, interitemSpacing: 8)
        .inset(by: .padding(15))
    )

    itemProvider.animator = FadeAnimator()
    itemProvider.tapHandler = { (context) in
      self.jump(to: context.data, index: context.index)
    }

    collectionView.provider = ComposedProvider(sections: [itemProvider])
  }

  func jump(to item: EmployeeManagementModel, index: Int) {
    if index == 0 {
      let vc = EmployeeInfoListViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
      return
    }

    let vc = DepartmentInfoListViewController()
    vc.title = item.name
    vc.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(vc, animated: true)
  }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

struct EmployeeManagementModel {
  let name: String
}

extension EmployeeManagementModel {
  static let items: [EmployeeManagementModel] = [
    .init(name: "员工信息"),
    .init(name: "财务部"),
    .init(name: "管理部"),
    .init(name: "工程部"),
    .init(name: "综合部"),
    .init(name: "采购部"),
    .init(name: "人事部"),
  ]
}
